#include <algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;

struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
 };

class Solution {
public:
    long long kthLargestLevelSum(TreeNode* root, int k) {
        queue<TreeNode*>tag;
        vector<long long>res;
        tag.push(root);
        while(!tag.empty())
        {
            int size=tag.size();
            int ans=0;
            for(int i=1;i<=size;i++)
            {
                TreeNode*tmp=tag.front();
                tag.pop();
                ans+=tmp->val;
                if(tmp->left)tag.push(tmp->left);
                if(tmp->right)tag.push(tmp->right);
            }
            res.push_back(ans);

        }
        sort(res.begin(),res.end(),[](auto const &c1,auto const &c2){
            return c1>=c2;
        });
        if(res.size()<k)return -1;
        else return res[k-1];

    }
};